﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CSWriter.Properties;
using CommunityServerRest;
using CSWriter.Blogs;

namespace CSWriter {
	public partial class SelectBlog : Form {

		public BlogManager BlogManager { get; set; }

		public BlogInfo SelectedBlog { get; set; }

		public SelectBlog(BlogManager manager) {
			InitializeComponent();
			BlogManager = manager;
		}

		private void SelectDefaultBlog_Load(object sender, EventArgs e) {
			try {
				BlogList blogs = BlogManager.GetBlogs();
				listView.BeginUpdate();
				//listView.Items.Clear();
				foreach (Blog blog in blogs.Blogs) {
					ListViewItem item = new ListViewItem(blog.Name);
					item.ImageIndex = 0;
					item.Tag = blog;
					listView.Items.Insert(0, item);

					if (SelectedBlog != null) {
						if (blog.Id==SelectedBlog.Id) {
							item.Selected = true;
						} else {
							item.Selected = false;
						}
					}
				}
				listView.EndUpdate();

			} catch {
				MessageBox.Show(string.Concat(Resources.UnableToRetrieveBlogs, Resources.CheckCredentials));
				Close();
			}
		}

		private void menuItem1_Click(object sender, EventArgs e) {

			DialogResult = DialogResult.Cancel;
			Close();
		}

		private void select_Click(object sender, EventArgs e) {
			ListViewItem selected = listView.Items[listView.SelectedIndices[0]];

			SelectedBlog = new BlogInfo((Blog)selected.Tag);
			DialogResult = DialogResult.OK;
			Close();
		}

		private void SelectDefaultBlog_KeyDown(object sender, KeyEventArgs e) {
			if ((e.KeyCode == System.Windows.Forms.Keys.Up)) {
				// Up
			}
			if ((e.KeyCode == System.Windows.Forms.Keys.Down)) {
				// Down
			}
			if ((e.KeyCode == System.Windows.Forms.Keys.Left)) {
				// Left
			}
			if ((e.KeyCode == System.Windows.Forms.Keys.Right)) {
				// Right
			}
			if ((e.KeyCode == System.Windows.Forms.Keys.Enter)) {
				menuItem1_Click(this, System.EventArgs.Empty);
			}

		}
	}
}